package com.huluobo.a2202mvvm_im_douyindemo.addGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.huluobo.a2202mvvm_im_douyindemo.R;
import com.huluobo.a2202mvvm_im_douyindemo.adapter.AddGroupAdapter;
import com.huluobo.a2202mvvm_im_douyindemo.adapter.JoinedGroupAdapter;
import com.huluobo.a2202mvvm_im_douyindemo.chat.ChatActivity;
import com.huluobo.a2202mvvm_im_douyindemo.databinding.ActivityAddGroupBinding;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseMVVMActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;
import com.hyphenate.chat.EMOptions;

import java.util.ArrayList;
import java.util.List;

public class AddGroupActivity extends BaseMVVMActivity<ActivityAddGroupBinding, AddGroupViewModel> {

    private AddGroupAdapter addGroupAdapter;

    private List<EMGroupInfo> emGroupInfoList = new ArrayList<>();

    private JoinedGroupAdapter joinedGroupAdapter;

    private List<EMGroup> emGroupList = new ArrayList<>();

    @Override
    protected void initData() {
        viewModel.createGroupSuccess.observe(this, ToastUtils::showLong);

        viewModel.createGroupFailed.observe(this, s -> {
            Log.i("TagA", "创建群失败:" + s);
        });

        viewModel.findGroupSuccess.observe(this, list -> {
            emGroupInfoList.clear();
            emGroupInfoList.addAll(list);
            dataBinding.addGroupRv.setAdapter(addGroupAdapter);
        });

        viewModel.findGroupFailed.observe(this, s -> {
            Log.i("TagA", "查找公开群失败:" + s);
        });

        viewModel.joinGroupSuccess.observe(this, ToastUtils::showLong);

        viewModel.joinGroupFailed.observe(this, ToastUtils::showLong);

        viewModel.findJoinedGroupSuccess.observe(this, list -> {
            emGroupList.clear();
            emGroupList.addAll(list);
            dataBinding.addGroupRv.setAdapter(joinedGroupAdapter);
        });

        viewModel.findJoinedGroupFailed.observe(this, s -> {
            Log.i("TagA", "查找加入过的群失败:" + s);
        });
    }

    @Override
    protected void intiView() {
        dataBinding.addGroupRv.setLayoutManager(new LinearLayoutManager(this));
        addGroupAdapter = new AddGroupAdapter(emGroupInfoList);
        joinedGroupAdapter = new JoinedGroupAdapter(emGroupList);

        joinedGroupAdapter.addChildClickViewIds(R.id.item_findJoinedGroup_btn);
        joinedGroupAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.item_findJoinedGroup_btn) {
                String conversationId = emGroupList.get(position).getGroupId();
                Intent intent = new Intent(this, ChatActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("conversationId", conversationId);
                bundle.putInt("chatType", 2);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


        addGroupAdapter.setOnAddClick(groupId -> {
            viewModel.joinGroup(groupId);
        });

        dataBinding.addGroupCreateGroupBtn.setOnClickListener(v -> {
            EMGroupOptions option = new EMGroupOptions();
            option.maxUsers = 100;
            option.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;

            if (TextUtils.isEmpty(dataBinding.addGroupGroupName.getText().toString())) {
                dataBinding.addGroupGroupName.setError("请输入群名字");
                return;
            }

            if (TextUtils.isEmpty(dataBinding.addGroupGroupDesc.getText().toString())) {
                dataBinding.addGroupGroupDesc.setError("请输入群的描述");
                return;
            }

            viewModel.createGroup(
                    dataBinding.addGroupGroupName.getText().toString(),
                    dataBinding.addGroupGroupDesc.getText().toString(),
                    new String[]{EMClient.getInstance().getCurrentUser()},
                    "",
                    option
            );
        });

        dataBinding.addGroupFindGroupBtn.setOnClickListener(v -> {
            viewModel.findGroup(10, null);
        });

        dataBinding.addGroupFindJoinedGroupBtn.setOnClickListener(v -> viewModel.findJoinedGroup());

    }

    @Override
    protected Class<AddGroupViewModel> getViewModel() {
        return AddGroupViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_group;
    }
}